package com.xuelang.pipeline.infrastructure.application.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuelang.pipeline.client.application.dto.TraitQuery;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.domain.application.Trait;
import com.xuelang.pipeline.domain.application.repository.TraitRepository;
import com.xuelang.pipeline.infrastructure.application.converter.TraitToTraitDOConverter;
import com.xuelang.pipeline.infrastructure.application.dataobject.TraitDO;
import com.xuelang.pipeline.infrastructure.application.mapper.TraitMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 运维特征仓库实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Repository
public class TraitRepositoryImpl implements TraitRepository {

    @Autowired
    TraitMapper traitMapper;

    @Override
    public PageRecord<Trait> pageList(long currentPage, long pageSize, TraitQuery traintQuery) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<TraitDO> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<TraitDO> wrapper = Wrappers.lambdaQuery(TraitDO.class);
        if (traintQuery != null) {
            if (StringUtils.isNotBlank(traintQuery.getApplicationCode())) {
                wrapper.eq(TraitDO::getApplicationCode, traintQuery.getApplicationCode());
            }
            if (StringUtils.isNotBlank(traintQuery.getComponentLabel())) {
                wrapper.eq(TraitDO::getComponentLabel, traintQuery.getComponentLabel());
            }
            if (StringUtils.isNotBlank(traintQuery.getEnvironmentUuid())) {
                wrapper.eq(TraitDO::getEnvironmentUuid, traintQuery.getEnvironmentUuid());
            }
            if (traintQuery.getReplicas() != null && traintQuery.getReplicas() != 0) {
                wrapper.like(TraitDO::getReplicas, traintQuery.getReplicas());
            }
            if (StringUtils.isNotBlank(traintQuery.getPort())) {
                wrapper.like(TraitDO::getPort, traintQuery.getPort());
            }
            if (StringUtils.isNotBlank(traintQuery.getCpu())) {
                wrapper.like(TraitDO::getCpu, traintQuery.getCpu());
            }
            if (StringUtils.isNotBlank(traintQuery.getInternalStorage())) {
                wrapper.like(TraitDO::getInternalStorage, traintQuery.getInternalStorage());
            }
            if (StringUtils.isNotBlank(traintQuery.getDomainName())) {
                wrapper.like(TraitDO::getDomainName, traintQuery.getDomainName());
            }
        }
        traitMapper.selectPage(page, wrapper);
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords().stream().map(TraitToTraitDOConverter.INSTANCE::traintDOToTrait).collect(Collectors.toList())
        );
    }

    @Override
    public List<Trait> list(TraitQuery traitQuery) {
        LambdaQueryWrapper<TraitDO> wrapper = Wrappers.lambdaQuery(TraitDO.class);
        if (traitQuery != null) {
            if (StringUtils.isNotBlank(traitQuery.getApplicationCode())) {
                wrapper.eq(TraitDO::getApplicationCode, traitQuery.getApplicationCode());
            }
            if (StringUtils.isNotBlank(traitQuery.getComponentLabel())) {
                wrapper.eq(TraitDO::getComponentLabel, traitQuery.getComponentLabel());
            }
            if (StringUtils.isNotBlank(traitQuery.getEnvironmentUuid())) {
                wrapper.eq(TraitDO::getEnvironmentUuid, traitQuery.getEnvironmentUuid());
            }
            if (traitQuery.getReplicas() != null && traitQuery.getReplicas() != 0) {
                wrapper.like(TraitDO::getReplicas, traitQuery.getReplicas());
            }
            if (StringUtils.isNotBlank(traitQuery.getPort())) {
                wrapper.like(TraitDO::getPort, traitQuery.getPort());
            }
            if (StringUtils.isNotBlank(traitQuery.getCpu())) {
                wrapper.like(TraitDO::getCpu, traitQuery.getCpu());
            }
            if (StringUtils.isNotBlank(traitQuery.getInternalStorage())) {
                wrapper.like(TraitDO::getInternalStorage, traitQuery.getInternalStorage());
            }
            if (StringUtils.isNotBlank(traitQuery.getDomainName())) {
                wrapper.like(TraitDO::getDomainName, traitQuery.getDomainName());
            }
        }
        return traitMapper.selectList(wrapper).stream()
                .map(TraitToTraitDOConverter.INSTANCE::traintDOToTrait).collect(Collectors.toList());
    }

    @Override
    public Boolean save(Trait trait) {
        return traitMapper.insert(TraitToTraitDOConverter.INSTANCE.traintToTraitDO(trait)) == 1;
    }

    @Override
    public Boolean update(Trait traint) {
        return traitMapper.update(
                TraitToTraitDOConverter.INSTANCE.traintToTraitDO(traint),
                Wrappers.lambdaQuery(TraitDO.class).eq(TraitDO::getUuid, traint.getUuid())) == 1;
    }

    @Override
    public Boolean delete(Trait traint) {
        return traitMapper.delete(Wrappers.lambdaQuery(TraitDO.class)
                .eq(TraitDO::getUuid, traint.getUuid())) == 1;
    }

    @Override
    public Trait query(Trait trait) {
        TraitDO traintDO = traitMapper.selectOne(Wrappers.lambdaQuery(TraitDO.class).eq(TraitDO::getUuid, trait.getUuid()));
        return TraitToTraitDOConverter.INSTANCE.traintDOToTrait(traintDO);
    }

    @Override
    public Boolean deleteByComponentLabel(String componentLabel) {
        return traitMapper.delete(Wrappers.lambdaQuery(TraitDO.class)
                .eq(TraitDO::getComponentLabel, componentLabel)) > 0;
    }

}
